import {localRead, localSave} from "./util"


// 定义全局方法 放弃store

export const getColumns = (columnList) => {
    // 调整如果语言切换则需要修改
    let uuid = getPath()
    let localColumns = localRead(uuid)
    if(localColumns==='') {
        if(columnList===undefined){
            return []
        }
        // 添加hidden属性
        columnList.forEach(item=>{
            delete item.__id
            item.hidden = false
        })
        localSave(uuid, JSON.stringify(columnList))
        return columnList
    }else {
        return JSON.parse(localRead(uuid))
    }

}

export const getPath =()=> {
    let urls = window.location.href.split('/')
    let count = urls.length
    return urls[count - 1]
}

export const operatColumn=(keys,isHidden)=> {

    let columns = getColumns()
    columns.forEach(item=>{
        if(keys.indexOf(item.key)>=0){
            item.hidden = isHidden
        }
    })

    localSave(getPath(), JSON.stringify(columns))
}

export const hideColumn=(key)=> {
    // let list = columns; // 备份原列字段
    // let index=columns.findIndex(function (item) {
    //   return item.key===key
    // })
    // //调用list.splice(元素的索引，元素的个数)
    // columns.splice(index,1);
    // let hideCol = list.filter(s => s.key === key); // 获取删除列对象
    // localSave(pageCode, JSON.stringify(hideCol))
    // return columns;
    let columns = getColumns()
    columns.some((item, i) => {
        if (item.key === key) {
            columns.splice(i, 1)
            return true;
        }
    })
    localSave(getPath(), JSON.stringify(columns))
}

export const showColumn=(column,index)=> {
    let columns = JSON.parse(localRead(getPath()))
    // array.splice(2, 0, "three");
    columns.splice(index, 0, column)
    localSave(getPath(), JSON.stringify(columns))
}